package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostBrowseDao;
import ysq.xyg.host.model.dto.HostBrowseItemDto;
import ysq.xyg.host.model.po.HostBrowse;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostBrowseVo;
import ysq.xyg.host.service.HostBrowseService;

import java.util.Date;
import java.util.List;

/**
 * 房产-楼盘浏览service实现
 *
 * @author congcong tao
 * @date 2019-12-11 15:25:23
 */
@Service("hostBrowseService")
public class HostBrowseServiceImpl extends BaseServiceImpl<HostBrowse> implements HostBrowseService {

    @Autowired
    private HostBrowseDao hostBrowseDao;

    @Autowired
    public void setBaseDao(HostBrowseDao hostBrowseDao) {
        this.baseDao = hostBrowseDao;
    }

    /**
     * 分页查询
     *
     * @param hostBrowseVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:25:23
     */
    @Override
    public ReturnEntity page(HostBrowseVo hostBrowseVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<HostBrowse> list = acceptList(hostBrowseVo);
        PageInfo<HostBrowse> page = new PageInfo<HostBrowse>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostBrowseVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:25:23
     */
    @Override
    public ReturnEntity addOrUpdate(HostBrowseVo hostBrowseVo, SysUser currentUser) {
        boolean isUpate = true;
        HostBrowse hostBrowse;
        if (StrUtil.isEmpty(hostBrowseVo.getId())) {
            hostBrowse = new HostBrowse(currentUser);
            isUpate = false;
        } else {
            hostBrowse = selectByPrimaryKey(hostBrowseVo.getId());
            if (ObjectUtil.isNull(hostBrowse)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostBrowse.setUpdateTime(new Date());
        }
        hostBrowse.setItemId(hostBrowseVo.getItemId());
        hostBrowse.setMemberId(hostBrowseVo.getMemberId());
        hostBrowse.setNum(hostBrowseVo.getNum());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(hostBrowse);
        } else {
            num = insert(hostBrowse);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:25:23
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostBrowse hostBrowse = selectByPrimaryKey(id);
        if (hostBrowse != null) {
            hostBrowse.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostBrowse.setUpdateTime(new Date());
            int num = updateByPrimaryKey(hostBrowse);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostBrowseVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-11 15:25:23
     */
    @Override
    public List<HostBrowse> acceptList(HostBrowseVo hostBrowseVo){
        Example example = new Example(HostBrowse.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostBrowseVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostBrowseVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostBrowseVo.getItemId())) {
            criteria.andEqualTo("itemId", hostBrowseVo.getItemId());
        }
        if (StringUtil.isNotEmpty(hostBrowseVo.getMemberId())) {
            criteria.andEqualTo("memberId", hostBrowseVo.getMemberId());
        }
        example.setOrderByClause(" update_time desc");
        return hostBrowseDao.selectByExample(example);
    }

    @Override
    public List<HostBrowseItemDto> selectHostBrowseItemList(String memberId, String cityId, int size) {
        return hostBrowseDao.selectHostBrowseItemList(memberId, cityId, size);
    }

    @Override
    public ReturnEntity myBrowseHostItemPage(HostBrowseVo hostBrowseVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<HostBrowseItemDto> list = hostBrowseDao.selectEntityList(hostBrowseVo);
        PageInfo<HostBrowseItemDto> page = new PageInfo<>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }
}
